
<template>

    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <!--<el-breadcrumb-item><i class="el-icon-menu"></i> 表格</el-breadcrumb-item>-->
                <el-breadcrumb-item>用户列表</el-breadcrumb-item>
            </el-breadcrumb>

			<el-dialog :title="title" :visible.sync="dialogFormVisible">
			  <el-form ref="form" :model="form" label-width="80px">
                 <el-form-item label="员工编码">
                    <el-input maxlength="10" v-model="form.staffId" v-bind:disabled="is_show==false"></el-input>
                </el-form-item>
                <el-form-item label="用户名称">
                    <el-input maxlength="30" v-model="form.name" v-bind:disabled="is_show==false"></el-input>
                </el-form-item>
                 <el-form-item label="密码">
                    <el-input maxlength="50" v-model="form.password" type="password" v-bind:disabled="is_show==false"></el-input>
                </el-form-item>
                <el-form-item label="确认密码">
                    <el-input maxlength="50" v-model="form.password1" type="password" v-bind:disabled="is_show==false"></el-input>
                </el-form-item>
                <el-form-item label="部门">
                     <el-select v-model="departmentCode1" placeholder="筛选部门" v-bind:disabled="is_show==false" >
                 <el-option
                            v-for="item in departmentList"
                            :key="item.departmentCode"
                            :label="item.name"
                            :value="item.departmentCode">
                    </el-option>
            </el-select>
                </el-form-item>
                <el-form-item label="组">
	              <el-select v-model="groupIds" multiple placeholder="筛选小组" v-bind:disabled="is_show==false">
			      <el-option
				      v-for="item in options"
				      :key="item.groupId"
				      :label="item.name"
				      :value="item.groupId">
			      </el-option>
	  			</el-select>
  			</el-form-item>
                <el-form-item label="手机号">
                    <el-input maxlength="11" v-model="form.phone" v-bind:disabled="is_show==false"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" v-if="is_show" @click="onSubmit">提交</el-button>
                    <el-button v-if="is_show" @click="cancel">取消</el-button>
                    <el-button v-if="is_show==false" @click="cancel">关闭</el-button>
                </el-form-item>
            </el-form>
			</el-dialog>
        </div>

        <div class="handle-box">
            <el-button type="primary" icon="edit" class="handle-del mr10" @click="save()">新增</el-button>

        </div>

        <div class="handle-box">
            <el-select v-model="departmentCode" clearable placeholder="筛选部门" class="handle-select mr10">
                 <el-option
                            v-for="item in departmentList"
                            :key="item.departmentCode"
                            :label="item.name"
                            :value="item.departmentCode">
                    </el-option>
            </el-select>

            <el-select v-model="groupId" clearable placeholder="筛选小组" class="handle-select mr10">
                 <el-option
                            v-for="item in options"
                            :key="item.groupId"
                            :label="item.name"
                            :value="item.groupId">
                    </el-option>
            </el-select>
            <el-input v-model="userName" placeholder="用户名称" class="handle-input mr10" icon="circle-close" :on-icon-click="handleIconClick"></el-input>
            <el-button type="primary" icon="search" @click="search">搜索</el-button>
        </div>

        <el-table :data="data" border style="width: 100%" ref="multipleTable" @selection-change="handleSelectionChange">
           <el-table-column v-if="false"  prop="userId" label="userId"  width="150">
            </el-table-column>
            <el-table-column prop="staffId" label="员工编号"  width="150">
                <template slot-scope='scope'>
                    <a style="border-bottom:solid 1px; cursor: pointer"  @click="detailed(scope.$index, scope.row)"> {{scope.row.staffId}}</a>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="120">
            </el-table-column>
            <el-table-column prop="deparimentName" label="部门" >
            </el-table-column>

             <el-table-column prop="phone" label="手机">
            </el-table-column>
             <el-table-column label="操作" width="180">
                <template slot-scope="scope">
                    <el-button size="small"
                            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="small" type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
       <div class="pagination">
            <el-pagination
                    @current-change ="handleCurrentChange"
                    @size-change="handleSizeChange"
                    layout="sizes,prev, pager, next,total"
                    :page-size="pageSize"
                    :page-sizes="[10,20,50]"
                    :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import Multiselect from 'vue-multiselect'
    export default {
    components: { Multiselect },
        data() {
            return {
                is_show:false,
                tableData: [],
                curPage: 1,
                pageSize: 10,
                total:0,
                multipleSelection: [],
                departmentCode:'',
                departmentCode1:'',
   			    departmentList:'',
                userName: '',
                delList: [],
                title:'',
                isSearch: false,
                options: [
                ],
        		groupIds: '',
        		groupId: '',
        		dialogFormVisible: false,
           		form: {
                    userId:null,
                	staffId:'',
                    name: '',
                    phone: '',
                    groupIds: '',
                    departmentCode: '',
                    departmentCode1: '',
                    password:''

                },
        		formLabelWidth: '120px'
            }
        },
        created(){
            this.getData();
        },
        computed: {
            data(){
                const self = this;
                return self.tableData.filter(function(d){

                    let is_del = false;
                    for (let i = 0; i < self.delList.length; i++) {

                        if(d.name === self.delList[i].name){
                            is_del = true;
                            break;
                        }
                    }
                    return d;
                })
            },
            // total(){
            // 	return this.total;
            // }
        },
        methods: {
            handleCurrentChange(val){
                this.curPage = val;
                this.getData();
            },
              handleSizeChange (val) {
                 this.pageSize = val;
                 this.getData();

          } ,
          cancel() {
                let self = this;
                self.dialogFormVisible = false ;

            },
            getData(){
                let self = this;
                if(process.env.NODE_ENV === 'development'){
                    self.url = '/ms/table/list';
                };


	           var searchCriteria = {};
	           searchCriteria.userName = '%' + (self.userName||'') + '%';
	           searchCriteria.departmentCode = '%' + (self.departmentCode||'') + '%';
	           searchCriteria.groupId = '%' + (self.groupId||'') + '%';

               self.$axios.post("../adm/user/page?size="+self.pageSize+"&page="+(self.curPage-1), searchCriteria).then((res) => {
                    self.tableData = res.data;
                    self.total = res.total;

                });

                self.$axios.get("../adm/department/list", {page:self.curPage}).then((res) => {
                    self.departmentList = res.data;

                })



                self.$axios.get("../adm/group/allGroup", {page:self.curPage}).then((res) => {
                    self.options = res.data;

                })

            },
            search(){
	           let self = this;
	           var searchCriteria = {};
	           searchCriteria.userName = '%' + (self.userName||'') + '%';
	           searchCriteria.departmentCode = '%' + (self.departmentCode||'') + '%';
	           searchCriteria.groupId = '%' + (self.groupId||'') + '%';

               self.curPage = 0;
               self.$axios.post("../adm/user/page?size="+self.pageSize+"&page="+self.curPage, searchCriteria).then((res) => {
                    self.tableData = res.data;
                    self.total =  res.data.total;
                });
            },
            formatter(row, column) {
                return row.name;
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            handleEdit(index, row) {

                 let self = this;
                 self.dialogFormVisible = true;
                 self.is_show = true;
                 self.title = "编辑用户";
                 self.$axios.get('../adm/user/userDetails?userId='+row.userId, {page:self.curPage}).then((res) => {
                 self.form = res.data;
                 self.departmentCode1 = res.data.departmentCode;
                 self.groupIds = res.data.userGroupIds;
                 self.form.password1  = res.data.password;
                })
            },
            detailed(index, row) {

                 let self = this;
                 self.is_show = false;
                 self.dialogFormVisible = true;
                 self.title = "用户详情";
                 self.$axios.get('../adm/user/detail?userId='+row.userId, {page:self.curPage}).then((res) => {
                 self.form = res.data;
                 self.departmentCode1 = res.data.departmentCode;
                 self.groupIds = res.data.userGroupIds;
                 self.form.password1  = res.data.password;
                })
            },
             save() {
                let self = this;
                self.is_show = true;
           	 	self.dialogFormVisible = true ;
                self.title = "添加用户";
           	 	self.form.staffId = '';
             	self.form.name = '';
             	self.form.phone = '';
                self.form.departmentCode = '';
                self.groupIds = [];
                self.form.password = '';
                self.form.password1 = '';
                self.departmentCode1 = '';

                self.$axios.get("../adm/department/list", {page:self.curPage}).then((res) => {
                self.departmentList = res.data;

                })
              self.$axios.get("../adm/group/allGroup", {page:self.curPage}).then((res) => {
                    self.options = res.data;

                })
	   	    },
            handleDelete(index, row) {
                let self = this;
                 self.$axios.get('../adm/user/delete?userId='+row.userId, {page:self.curPage}).then((res) => {
                    self.getData();
                    this.$message.error('删除成功！');

                  })

            },
            handleIconClick(){
                let self = this;
                this.userName = "";
            },
             onSubmit() {

		        var self = this;
		  		self.form.departmentCode = self.departmentCode1;
		  		self.form.groupIds = self.groupIds;

		         if(!self.form.staffId){
		       		 this.$message.success('staffId不可为空！');
		       		 return;
		        }
		          if(!self.form.name){
		       		 this.$message.success('用户名不可为空！');
		       		 return;
		        }
		         if(!self.form.password ){
		       		 this.$message.success('密码不可为空！');
		       		 return;
		        }
		          if(!self.form.password1){
		       		 this.$message.success('确认密码不可为空！');
		       		 return;
		        }
		         if(self.form.password1 != self.form.password){
		       		 this.$message.success('两次密码不一致！');
		       		 return;
		        }
                 if(self.form.password1.length<6){
                     this.$message.success('密码不得少于6位！');
                     return;
                 }
		          if(!self.form.departmentCode){
		       		 this.$message.success('部门不可为空！');
		       		 return;
		        }
		          if(!self.form.groupIds){
		       		 this.$message.success('小组不可为t空！');
		       		 return;
		        }
		        self.$axios({
		        method: 'post',
		        url: '../adm/user/save',
		        handleAs:"json",
		        headers:{'Content-Type':'application/json'},
		        data: JSON.stringify(self.form)
		    }).then(function (res) {
                if(!res.data){
                    self.$message.success('StaffId已存在，不可添加！');
                    return;
                 }
                 self.dialogFormVisible = false ;
                 self.$message.success('操作成功！');
                 self.getData();
		     }).catch(function (err) {
		     })
	      },
          handleSelectionChange(val) {
              this.multipleSelection = val;
            }
        }
    }
</script>
<style src="vue-multiselect/dist/vue-multiselect.min.css"></style>
<style scoped>
.multiselect1{
	width:15%;
	}
.handle-box{
    margin-bottom: 20px;
}
.handle-select{
    width: 120px;
}
.handle-input{
    width: 300px;
    display: inline-block;
}
</style>
